package com.spddr.pmos.cxf.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spddr.pmos.cxf.JaxrsTradeResultDetail;
import com.spddr.pmos.domain.TreeLevel;
import com.spddr.pmos.jpa.ContractPlan;
import com.spddr.pmos.jpa.TradeResultDetail;
import com.spddr.pmos.repos.ContractPlanRepos;
import com.spddr.pmos.repos.TradeResultDetailRepos;

@Service
public class JaxrsTradeResultDetailImpl implements JaxrsTradeResultDetail{

	@Autowired
	TradeResultDetailRepos tradeResultDetailRepos;
	@Autowired
	ContractPlanRepos plan;
	
	@Override
	public JpaRepository<TradeResultDetail, Long> getRepository() {
		return tradeResultDetailRepos;
	}

	@Override
	public List<TradeResultDetail> findAll(long id,TreeLevel level, String date){
		List<TradeResultDetail> result = new ArrayList<TradeResultDetail>();
		/*//格式化时间参数
		String[] dates = date.split("-");
		date = dates[0]+"-"+dates[1]+"-02";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		Date dateCon = new Date();
		try {
			dateCon = format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}*/
		List<ContractPlan> contractPlans = plan.findByOrgPlantAndMonthAndDisable(id,date,1);
		result = tradeResultDetailRepos.findByDisableAndContractPlanIn(1,contractPlans);
		return result;
	}
	
	//逻辑删除
	public void delete( Long id) {
		TradeResultDetail detail = tradeResultDetailRepos.getOne(id);
		detail.setDisable(0);
		getRepository().save(detail);
	}

	@Override
	public String getContractPlantByCon(long orgPlant,String month) {
		String result = "^";
		List<ContractPlan> plans = plan.findByOrgPlantAndMonthAndDisable(orgPlant,month, 1);
		if(plans.size()>0){
			for (ContractPlan contractPlan : plans) {
				/*String planMsg = contractPlan.getId().toString()+"_"+
						contractPlan.getTradeVariety()!=null?contractPlan.getTradeVariety().getName():" " +" "+
						contractPlan.getTradeWay()!=null?contractPlan.getTradeWay().getName():" " +" "+
						contractPlan.getTradeVariety()!=null?contractPlan.getTradeVariety().getName():" " +" "+
						contractPlan.getElectricityType()!=null?contractPlan.getElectricityType().getName():" " +" "+
						contractPlan.getTradeArea()!=null?contractPlan.getTradeArea().getName():" ";*/
				String planMsg =  contractPlan.getId().toString()+"_";
				
				if(contractPlan.getTradeVariety()!=null){
					planMsg += contractPlan.getTradeVariety().getName()+" ";
				}
				if(contractPlan.getTradeWay()!=null){
					planMsg += contractPlan.getTradeWay().getName()+" ";
				}
				if(contractPlan.getTradeVariety()!=null){
					planMsg += contractPlan.getTradeVariety().getName()+" ";
				}
				if(contractPlan.getElectricityType()!=null){
					planMsg += contractPlan.getElectricityType().getName()+" ";
				}
				if(contractPlan.getTradeArea()!=null){
					planMsg += contractPlan.getTradeArea().getName()+" ";
				}
				
				result += planMsg + "@";
			}
			result = result.substring(0,result.length()-1);
			//result += "!";
		}
		result += "!";
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public void saveList(String data) {
		if(data != null && !"".equals(data)){
			data = data.replace("^", "[");
			data = data.replace("*", "]");
			ObjectMapper mapper = new ObjectMapper();
			
			try {
				List<TradeResultDetail> details = (List<TradeResultDetail>)mapper.readValue(data,new TypeReference<List<TradeResultDetail>>() { });
				if(details.size()>0){
					for(TradeResultDetail detail:details){
						detail.setContractPlan(plan.getOne(Long.valueOf(detail.getDisable())));
						detail.setDisable(1);
						tradeResultDetailRepos.save(detail);
					}
				}
			}catch(Exception  e){
				e.printStackTrace();
			}
			}
	}
}
